package br.com.materiais.dao;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import br.com.materiais.factory.MaterialConnectionFactory;
import br.com.materiais.model.MaterialModel;

public class MateriaisDao 
{
	private Session session = null;
	Transaction tx = null;
	public MateriaisDao()
	{
		
	}
	
	
	@SuppressWarnings("unchecked")
	public List getListaMateriais()
	{
		session = MaterialConnectionFactory.getInstance( ); 
	 
	  {
		String SQLCONSULTA = "from MaterialModel m ORDER BY m.nome" ;
		
		
		List list = session.createQuery( SQLCONSULTA ).list();
		
		return list;
	  }
		
	}
	
	@SuppressWarnings("unchecked")
	public List getListaMateriais(MaterialModel pModel)
	{
	   
		String SQLFROM = "FROM MaterialModel as m ";
		String SQLWHERE = "";
		Query query = null;
	    session = MaterialConnectionFactory.getInstance( );
	    
	    if(!pModel.getNome().equals(""))
	    {
	    	if(SQLWHERE.length() > 1 )
	    		SQLWHERE = SQLWHERE + " AND m.nome='" + pModel.getNome() +"'";
	    	else
	    		SQLWHERE = "WHERE m.nome='" + pModel.getNome()+ "'";
	    }
	    
	    if(!pModel.getCodigo().equals(""))
	    {
	    	if(SQLWHERE.length() > 1)
	    		SQLWHERE = SQLWHERE + " AND m.codigo='" + pModel.getCodigo() + "'";
	    	else
	    		SQLWHERE = " WHERE m.codigo='" + pModel.getCodigo() + "'";
	    }
	    
	    if(!pModel.getEstante().equals(""))
	    {
	    	if(SQLWHERE.length() > 1)
	    		SQLWHERE = SQLWHERE + " AND m.estante='" + pModel.getEstante() +"'";
	    	else
	    		SQLWHERE = " WHERE m.estante='" + pModel.getEstante() + "'";
	    }
	    
	    if(!pModel.getPrateleira().equals(""))
	    {
	    	if(SQLWHERE.length() > 1)
	    		SQLWHERE = SQLWHERE + " AND m.prateleira='" + pModel.getPrateleira() + "'";
	    	else
	    		SQLWHERE = "WHERE m.prateleira='" + pModel.getPrateleira() + "'";
	    }
	    
	    query = session.createQuery(SQLFROM + SQLWHERE);
	    
		return query.list(); 
	}
	
	public void salvarMateriais(MaterialModel pModel)
	{   
		 session = MaterialConnectionFactory.getInstance( );
		   try
		   {
			   tx = session.beginTransaction();
			   pModel.setCriado(new Timestamp(System.currentTimeMillis()));
			   session.save(pModel);
			   tx.commit();
		   }
		   
		   catch(HibernateException hex)
		   {
			   hex.printStackTrace();
			   tx.rollback();
		   }
		   
		   finally
		   {
			   session.close();
		   }
			
		
	}
	public void editarMateriais(MaterialModel pModel)
	{
	   session = MaterialConnectionFactory.getInstance( );
	   try
	   {
		   tx = session.beginTransaction();
		   session.update(pModel);
		   tx.commit();
	   }
	   
	   catch(HibernateException hex)
	   {
		   hex.printStackTrace();
		   tx.rollback();
	   }
	   
	   finally
	   {
		   session.close();
	   }
		
	}
	
	public void excluirMateriais(MaterialModel material)
	{
		session = MaterialConnectionFactory.getInstance( );
		   try
		   {
			   tx = session.beginTransaction();
			   session.delete(material);
			   tx.commit();
		   }
		   
		   catch(HibernateException hex)
		   {
			   hex.printStackTrace();
			   tx.rollback();
		   }
		   
		   finally
		   {
			   session.close();
		   }
		
	}
	public boolean isCodigoMaterialExiste(MaterialModel pMaterial)
	{
		String SQL = "FROM MaterialModel AS m WHERE m.nome=:nome";
		session = MaterialConnectionFactory.getInstance();
		
		Query query = session.createQuery(SQL);
		query.setParameter("nome", pMaterial.getNome());
		
		if(query.list().isEmpty())
			return false;

		return true;
	}

}
